package _04_递归;

public class Hanoi {

    /**
     * 将n个盘子，从p1移动到p3
     * @param n
     * @param p1
     * @param p2
     * @param p3
     */
    void hanoi(int n,String p1,String p2,String p3){
        if(n<=1){
            move(n,p1,p3); //base case
            return;
        }
        //首先将n-1个盘子移动到p2
        hanoi(n-1,p1,p3,p2);
        //在将第n个盘子移动到p3
        move(n,p1,p3);
        //最后再将n-1个盘子移动到p3,此时它在p2上
        hanoi(n-1,p2,p1,p3);
    }

    /**
     * 将第no号盘子从from移动到to
     * @param no
     * @param from
     * @param to
     */
    void move(int no,String from,String to){
        System.out.println(no+"号盘子:"+from+"->"+to);
    }

    public static void main(String[] args) {
        Hanoi hanoi = new Hanoi();
        hanoi.hanoi(1,"A","B","C");
        hanoi.hanoi(2,"A","B","C");
        hanoi.hanoi(3,"A","B","C");
    }
}
